//
//  ShoppingCarTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/30.
//

import UIKit

class ShoppingCarTableViewCell: UITableViewCell {
    var selectBtn: UIButton!
    var googsImageView: UIImageView!
    var sellOutView: UIView!//售罄、已下架阴影
    var sellOutImageView: UIImageView!//售罄、已下架
    var titleLab: UILabel!
    var delLab: UILabel!
    var priceLab: UILabel!
    var subtractBtn: UIButton!//减
    var addBtn: UIButton!//添加到购物车按钮
    var numTextField: UITextField!//数量
    var line: UIView!//线
    var num: Int = 1
    var model: ShoppingCarListModel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.initUI()
        self.addLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI() {
        
        self.selectBtn = UIButton(frame: CGRect.zero)
        self.selectBtn.setImage(UIImage(named: "icon_shopingcar_normal"), for: .normal)
        self.selectBtn.setImage(UIImage(named: "icon_shopingcar_selected"), for: .selected)
        self.selectBtn.addTarget(self, action: #selector(selectBtnAntion(sender:)), for: .touchUpInside)
        self.contentView.addSubview(self.selectBtn)
        
        
       
        
        self.googsImageView = UIImageView(frame: CGRect.zero)
        self.googsImageView.contentMode = .scaleAspectFill
        self.googsImageView.clipsToBounds = true
        self.contentView.addSubview(self.googsImageView)
        
        self.sellOutView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.googsImageView.width, height: self.googsImageView.height))
        self.sellOutView.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0.2)
        self.googsImageView.addSubview(self.sellOutView)
        
        
        
        self.sellOutImageView = UIImageView(frame: CGRect(x: self.selectBtn.right + 8, y: 8, width:86, height: 86))
        self.sellOutImageView.layer.contentsGravity = kCAGravityResizeAspectFill//防止变形
        self.sellOutView.addSubview(self.sellOutImageView)
        
        
        
        self.titleLab = UILabel(frame: CGRect.zero)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.titleLab.numberOfLines = 2
        self.contentView.addSubview(self.titleLab)
        
        
        self.delLab = UILabel(frame: CGRect.zero)
        self.delLab.textColor = UIColor.init(hex: 0x939AA8)
        self.delLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        self.contentView.addSubview(self.delLab)
        
        
        
        self.priceLab = UILabel(frame: CGRect.zero)
        self.priceLab.textColor = Theme.titleLabColor
        self.priceLab.font = UIFont.init(name: Theme.PingFangSCSemibold, size: 18)
        self.contentView.addSubview(self.priceLab)
        
              
        
        
        //添加按钮
        self.addBtn = UIButton(frame: CGRect(x:kScreenWidth - 38, y: self.googsImageView.height - 14, width: 24, height: 24))
        self.addBtn.setImage(UIImage(named: "icon_add_car"), for: .normal)
        self.addBtn.contentHorizontalAlignment = .right
        self.addBtn.addTarget(self, action: #selector(addBtnClick), for: .touchUpInside)
        self.addBtn.contentVerticalAlignment = .bottom
        self.contentView.addSubview(addBtn)
        
        
        self.numTextField = UITextField(frame: CGRect(x: self.addBtn.left - self.addBtn.width, y: self.googsImageView.height - 12, width: 24, height: 24))
        self.numTextField.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.numTextField.textColor = Theme.titleLabColor
        self.numTextField.textAlignment = .center
        self.numTextField.keyboardType = .numberPad
        self.numTextField.delegate = self
        self.contentView.addSubview(self.numTextField)


        
        //减按钮
        self.subtractBtn = UIButton(frame: CGRect(x:self.numTextField.left - self.numTextField.width, y: self.googsImageView.height - 14, width: 24, height: 24))
        self.subtractBtn.setImage(UIImage(named: "icon_subtract_car"), for: .normal)
        self.subtractBtn.contentHorizontalAlignment = .right
        self.subtractBtn.addTarget(self, action: #selector(subtractBtnClick), for: .touchUpInside)
        self.subtractBtn.contentVerticalAlignment = .bottom
        self.contentView.addSubview(subtractBtn)
        
   
        self.line = UIView(frame: CGRect(x: 8, y: 109.5, width: kScreenWidth - 16, height: 0.5))
        self.line.backgroundColor = UIColor.init(hex: 0xD8D8D8)
        self.contentView.addSubview(self.line)

        
        
    }
    
    func addLayout(){
        
        self.selectBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(15)
            make.width.equalTo(30)
            make.bottom.equalTo(0)
        }
        
    
        
        self.googsImageView.snp.makeConstraints { (make) in
            make.left.equalTo(self.selectBtn.snp.right).offset(14)
            make.width.height.equalTo(66)
            make.centerY.equalTo(self.contentView.snp.centerY)
        }
        
        self.sellOutView.snp.makeConstraints { (make) in
            make.width.equalTo(self.googsImageView.snp.width)
            make.height.equalTo(self.googsImageView.snp.height)
        }
        
     
        
        self.sellOutImageView.snp.makeConstraints { (make) in
            make.width.equalTo(70)
            make.height.equalTo(70)
            make.centerY.equalTo(self.sellOutView.snp.centerY).offset(0)
            make.centerX.equalTo(self.sellOutView.snp.centerX).offset(0)
        }
        
        
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.equalTo(self.googsImageView.snp.right).offset(14)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(20)
        }
        
        
        self.delLab.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom).offset(4)
            make.left.equalTo(self.googsImageView.snp.right).offset(14)
            make.right.equalTo(-15)
            make.height.greaterThanOrEqualTo(20)
        }
        
        
        self.priceLab.snp.makeConstraints { make in
            make.top.equalTo(self.delLab.snp.bottom).offset(4)
            make.left.equalTo(self.googsImageView.snp.right).offset(14)
            make.right.equalTo(-15)
            make.height.greaterThanOrEqualTo(30)
            make.bottom.equalTo(-18)
        }
        
        
        self.addBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.priceLab.snp.top)
            make.right.equalTo(-14)
            make.width.height.equalTo(30)
        }

        self.numTextField.snp.makeConstraints { (make) in
            make.top.equalTo(self.priceLab.snp.top).offset(8)
            make.right.equalTo(self.addBtn.snp.right).offset(-20)
            make.width.equalTo(30)
            make.height.equalTo(24)
        }

        
        self.subtractBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.priceLab.snp.top)
            make.right.equalTo(self.numTextField.snp.left)
            make.width.height.equalTo(30)
        }
    
        self.line.snp.makeConstraints { (make) in
            make.left.equalTo(8)
            make.right.equalTo(-8)
            make.height.equalTo(1)
            make.bottom.equalTo(0)
        }
    }
    
    //model
    func setCarModel(carModel: ShoppingCarListModel){
        self.model =  carModel
        let imagesArray = (carModel.productImages ?? "").components(separatedBy: ",")
        if imagesArray.count > 0{
            carModel.productImages = imagesArray[0]
        }
        self.googsImageView.sd_setImage(with: URL.init(string: carModel.productImages ?? ""), placeholderImage: UIImage(named: "icon_default_place"))
        self.titleLab.text = carModel.productName ?? ""
        let price = XTManageCenter.showPriceInComma(source: (carModel.price ?? 0).cleanZero)
        self.priceLab.text = String(format: "%@ %@", XTManageCenter.currentSign(),price)
        self.delLab.text = carModel.params ?? ""
       
        self.num = carModel.quantity ?? 0
        self.numTextField.text = String(format: "%ld", self.num)
        
        self.selectBtn.isSelected = carModel.isSelect
        self.sellOutView.isHidden = true
        
        if carModel.availableStock == 0 {//售空
            self.sellOutView.isHidden = false
            let language = XTManageCenter.getLocalStringDataValue(aKey: UtilCenter.kAppLanguage)
            if language.contains("en"){//英文
                self.sellOutImageView.image = UIImage(named: "icon_sold_out_en")
            }else{
                self.sellOutImageView.image = UIImage(named: "icon_sold_out")
            }
            
        }
        
        if carModel.isShelf == 0 {//下架
            self.sellOutView.isHidden = false
            let language = XTManageCenter.getLocalStringDataValue(aKey: UtilCenter.kAppLanguage)
            if language.contains("en"){//英文
                self.sellOutImageView.image = UIImage(named: "icon_available_en")
            }else{
                self.sellOutImageView.image = UIImage(named: "icon_available")
            }
        }
        
        if carModel.isValid == false {//失效商品
            self.sellOutView.isHidden = false
            let language = XTManageCenter.getLocalStringDataValue(aKey: UtilCenter.kAppLanguage)
            if language.contains("en"){//英文
                self.sellOutImageView.image = UIImage(named: "icon_expired_en")
            }else{
                self.sellOutImageView.image = UIImage(named: "icon_expired")
            }
        }
    }
    
    
    //MARK: - Antion Methods
    @objc func selectBtnAntion(sender: UIButton) {
       
    }
    
   
    
    //添加
    @objc func addBtnClick() {
        if self.subtractBtn.isHidden == true {
            self.num = 1
        }else{
            self.num = self.num + 1;
        }

        self.numTextField.text = String(format: "%ld", self.num)
       
        
    }
    //减
    @objc func subtractBtnClick() {
        if (self.num > 1) {
            self.num = self.num - 1
            self.numTextField.text = String(format: "%ld", self.num)
        }else{
        }
       
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension ShoppingCarTableViewCell: UITextFieldDelegate{
    func textFieldDidEndEditing(_ textField: UITextField) {
     
    }
}
